Help! Office hours

Poll: What times are best for you for office hours?

Today is different

Ava will hold office hours from 5:00pm - 6:00pm EST.

Office hours will always be held at the same Zoom link.

Working with R – RStudio

RStudio is an Integrated Development Environment (IDE) for R

  • It helps the user effectively use R
  • Makes things easier
  • Is NOT a dropdown statistical tool (such as Stata)

RStudio logo

[source]

RStudio used to be the name of a company that is now called Posit.

RStudio

Easier working with R

  • Syntax highlighting, code completion, and smart indentation
  • Easily manage multiple working directories and projects

More information

  • Workspace browser and data viewer
  • Plot history, zooming, and flexible image and file export
  • Integrated R help and documentation
  • Searchable command history

RStudio

RStudio

Getting the editor

You must open a file first to save code to open the editor. These include for example R Scripts or R Markdown.

Working with R in R Studio - 2 major panes:

  1. The Source/Editor: “Analysis” Script + Interactive Exploration
    • Static copy of what you did (reproducibility)
    • Top by default
  2. The R Console: “interprets” whatever you type
    • Calculator
    • Try things out interactively, then add to your editor
    • Bottom by default

Source / Editor

  • Where files open to
  • Have R code and comments in them
  • Can highlight and press (CMD+Enter (Mac) or Ctrl+Enter (Windows)) to run the code

In a .R file (we call a script), code is saved on your disk

R Console

  • Where code is executed (where things happen)
  • You can type here for things interactively to test code
  • Code is not saved on your disk

RStudio

RStudio layout

To make an R Markdown file: click the green plus button -> R Markdown. We will do this together soon! It will look like this with text in it, unlike a script.

RStudio layout

Recall that a script was just empty

Scripts and R Markdown

Although people will use scripts often, and they are good for more programmatic purposes, we generally don’t recommend them for Public Health Researchers.

For data analyses, R Markdown files are generally superior because they allow you to check your code and write more info about your code.

RStudio Layout

If RStudio doesn’t look the way you want (or like our RStudio), then do:

RStudio –> Preferences –> Pane Layout

Workspace/Environment

Workspace/Environment

  • Tells you what objects are in R
  • What exists in memory/what is loaded?/what did I read in?

History

  • Shows previous commands. Good to look at for debugging, but don’t rely on it.
    Instead use RMarkdown!
  • Also type the “up” key in the Console to scroll through previous commands

Other Panes

  • Files - shows the files on your computer of the directory you are working in
  • Viewer - can view data or R objects
  • Help - shows help of R commands
  • Plots - pictures and figures
  • Packages - list of R packages that are loaded in memory

Let’s take a look at R Studio ourselves!

Lab: Starting with R and RMarkdown

💻 RStudio Lab

To do this lab we need to:

  1. Download the file at the link above by clicking on the link or typing in: https://jhudatascience.org/intro_to_r/modules/RStudio/lab/RStudio_Lab.Rmd

(Also on the website schedule page - Lab for day 1) 2) Find the downloaded file on your computer 3) Open the file in RStudio

This may require finding your downloads on your computer.

Recall that these videos can help:

If you have a PC: https://youtu.be/we6vwB7DsNU

If you have a Mac: https://www.youtube.com/watch?v=Ao9e0cDzMrE

You can find these on the resource page of the class website.

R Markdown file

R Markdown files (.Rmd) help generate reports that include your code and output. Think of them as fancier scripts.

  1. Helps you describe your code
  2. Allows you to check the output
  3. Can create many different file types

Create an R Markdown file

Go to File → New File → R Markdown or click the green add file button.

Code chunks

Within R Markdown files are code “chunks”

This is where you can type R code and run it!

Create Chunks

To create a new R code chunk:

Copy paste an existing chunk in the R Markdown file and replace the code OR

  1. Use the insert code chunk button at the top of RStudio.

  1. Select R (default) as the language:

Run previous chunks button

You can run all chunks above a specific chunk using this button:

Chunk settings

Chunk settings

You can specify if a chunk will be seen in the report or not.

Knit file to html

This will create a report from the R Markdown document!

knit

Useful R Studio Shortcuts

If you get annoyed by inline code previews in Markdown files:

In RStudio Click the Edit tab –> scroll down to Preferences… –> R Markdown

Uncheck the following:

knit

Recap of where code goes

  • you can test code in the console knit

  • you can save code in a chunk in the editor (Markdown file)

knit

Getting help from the preview

When you type in a function name, a pop up will preview documentation to help you. It also helps you remember the name of the function if you don’t remember all of it!

preview a function

preview a function

Get help with the help pane

help_pane

Getting Help with ?

If you know the name of a package or function:

Type ?package_name or ?function_name in the console to get information about packages and functions.

For example: ?readr or ?read_csv.

getting help

Double Question Mark

If you haven’t loaded a package yet into R than you may get a response that there is no documentation.

Typing in ??package_name can show you packages that you haven’t loaded yet.

help_pane

Summary

  • RStudio makes working in R easier
  • the Editor is for static code like scripts or R Markdown documents
  • The console is for testing code
  • R markdown documents are really helpful for lots of reasons!
  • R code goes within what is called a chunk (the gray box with a green play button)
  • Code chunks can be modified so that they show differently in reports

🏠 Class Website

💻 Lab

The End

Image by Gerd Altmann from Pixabay